//
//  CMDBRegisterVcodeTF.swift
//  CMDB
//
//  Created by 王磊 on 09/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBRegisterVcodeTF: CMDBSubRegisterTF {
    
    private var vcode: UIButton = UIButton(type: .Custom)
    
    private var timer: NSTimer?
    
    deinit {
        
        printLog("deinit")
    }
    private var count: Int = 0
}
extension CMDBRegisterVcodeTF {
    
    internal func destroyTimer() {
        
        timer?.invalidate()
        
        timer = nil
    }
    
}

extension CMDBRegisterVcodeTF {
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        addSubview(vcode)
    }
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        vcode.setTitle("获取验证码", forState: .Normal)
        
        vcode.setTitle("获取验证码", forState: .Selected)
        
        vcode.backgroundColor = UIColor.Hex6(0xff6d00, alpha: 54)
        
        vcode.setTitleColor(UIColor.Hex6(0xffffff, alpha: 0.54), forState: .Disabled)
        
        vcode.setTitleColor(UIColor.Hex6(0xffffff), forState: .Normal)
        
        vcode.setTitleColor(UIColor.Hex6(0xffffff), forState: .Selected)
        
        vcode.addTarget(self, action: #selector(vcodeHandler), forControlEvents: .TouchUpInside)
        
        vcode.titleLabel?.font = CMDB_12_FONT
        
        vcode.layer.cornerRadius = 3
        
        vcode.layer.masksToBounds = true
    }
}
extension CMDBRegisterVcodeTF {
    
    @objc private func vcodeHandler(sender: UIButton) {
        
        if superview is CMDBRegisterTopView {
            
            let sup = superview as! CMDBRegisterTopView
            
            if sup.phone.text.validPhoneNumber() {
                
                sender.enabled = false
                
                count = 60
                
                sender.setTitle("(\(count))s", forState: .Disabled)
                
                timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(timeChanged), userInfo: nil, repeats: true)
                
            }
        } else {
            
            let sup = superview
            
            let phone = sup!.subviews[0] as! CMDBSubRegisterTF
            
            if  phone.text.validPhoneNumber() {
                
                sender.enabled = false
                
                count = 60
                
                sender.setTitle("(\(count))s", forState: .Disabled)
                
                timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(timeChanged), userInfo: nil, repeats: true)
                
            }
        }
        
        if let target = unsureTarget {
            
            let argu = ArguInfo()
            
            argu.view = self
            
            argu.controlEvent = ControlEvent.Vcode
            
            TSRuntimeManager.getInstance().addTarget(target, andSel: unsureAction!, andAnyArgu: argu)
            
        }
        
    }
}

extension CMDBRegisterVcodeTF {
    
    @objc private func timeChanged(temp: NSTimer) {
        
        count -= 1
        
        printLog(count)
        
        if count < 0 {
            
            vcode.setTitle("获取验证码", forState: .Normal)
            
            destroyTimer()
            
        } else {
            
            vcode.setTitle("\(count)s", forState: .Disabled)
        }
    }
}
extension CMDBRegisterVcodeTF {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let size = "获取验证码".suitToSize(CGSizeMake(888, 888), fontSize: 15, lineSpacing: 3)
        
        vcode.snp_makeConstraints { (make) in
            
            make.right.equalTo(-5)
            
            make.centerY.equalTo(5)
            
            make.width.equalTo(size.width + 10)
            
            make.height.equalTo(size.height + 3)
            
        }
    }
}
